package cn.edu.xhu.library_management.area.service.impl;

import cn.edu.xhu.library_management.area.entity.LmArea;
import cn.edu.xhu.library_management.area.entity.LmFloor;
import cn.edu.xhu.library_management.area.entity.LmInfo;
import cn.edu.xhu.library_management.area.mapper.AreaMapper;
import cn.edu.xhu.library_management.area.mapper.FloorMapper;
import cn.edu.xhu.library_management.area.mapper.InfoMapper;
import cn.edu.xhu.library_management.area.service.FloorService;
import cn.edu.xhu.library_management.common.entity.ResultCode;
import cn.edu.xhu.library_management.common.exception.LibraryManagementException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: MMJ
 * @Date: 2022 - 11 - 13
 * @Description:
 */
@Service
public class FloorServiceImpl implements FloorService {
    @Resource
    private FloorMapper floorMapper;

    @Resource
    private AreaMapper areaMapper;
    @Resource
    private InfoMapper infoMapper;

    @Override
    public List<LmFloor> findFloorsByInfoId(Integer infoId) {
        return floorMapper.findFloorsByInfoId(infoId);
    }

    @Override
    public LmFloor findFloorById(Integer id) {
        return floorMapper.findFloorById(id);
    }

    @Override
    public boolean addFloor(LmFloor floor) {
        // 验证图书馆id是否存在
        LmInfo info = infoMapper.findInfoById(floor.getInfoId());
        if (info == null) {
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"该图书馆不存在!!!");
        }
        // 查看当前图书馆的所有楼层
        List<LmFloor> floors = findFloorsByInfoId(floor.getInfoId());
        // 查询添加的楼层是否存在
        LmFloor lmFloor = floors.stream().filter(flo -> flo.getIndex().equals(floor.getIndex())).findFirst().orElse(null);
        if (lmFloor != null) {
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"该楼层已经存在!!!");
        }
        return floorMapper.addFloor(floor) > 0;
    }

    @Override
    public boolean updateFloor(LmFloor floor) {
        // 验证图书馆id是否存在
        LmInfo info = infoMapper.findInfoById(floor.getInfoId());
        if (info == null) {
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"该图书馆不存在!!!");
        }
        // 查询修改的楼层是否存在，不存在则抛异常，存在则修改
        LmFloor lmFloor = findFloorById(floor.getId());
        if (lmFloor == null) {
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"该楼层不存在");
        }
        return floorMapper.updateFloor(floor) > 0;
    }

    @Override
    public boolean deleteFloorById(Integer id) {
        // 查询修改的楼层是否存在，不存在则抛异常，存在则删除
        LmFloor lmFloor = findFloorById(id);
        if (lmFloor == null) {
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"该楼层不存在");
        }
        return floorMapper.deleteFloorById(id) > 0;
    }

    @Override
    public List<LmFloor> findFloorsByType(Integer infoId, Integer type) {
        List<LmFloor> lmFloors = findFloorsByInfoId(infoId);
        List<LmFloor> floors = new ArrayList<>();
        lmFloors.forEach(floor -> {
            // 过滤指定类型的
            List<LmArea> areas = areaMapper.findAreasByFloorId(floor.getId());
            areas.stream().anyMatch(area -> {
                if (area.getType().equals(type)){
                    floors.add(floor);
                    return true;
                }
                return false;
            });
        });
        return floors;
    }


}
